<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.nameCn"
          clearable
          placeholder="名称"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.advertiseType"
          placeholder="廣告類型"
          clearable
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.advertiseType"
            :key="item.id"
            :label="item.dictValue"
            :value="item.dictKey"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        top="0"
        width="80%"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="100px" inline label-position="right">
          <el-row>
            <el-col :span="12">
              <el-form-item label="廣告圖類型">
                <el-select v-model="form.advertiseType" filterable placeholder="請選擇">
                  <el-option
                    v-for="item in dict.advertiseType"
                    :key="item.id"
                    :label="item.dictValue"
                    :value="item.dictKey"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="備注">
                <el-input v-model="form.note" :rows="1" type="textarea" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="開始時間" prop="startTime">
                <el-date-picker
                  v-model="form.startTime"
                  placeholder="選擇時間"
                  type="datetime"
                  :default-time="form.startTime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="結束時間" prop="endTime">
                <el-date-picker
                  v-model="form.endTime"
                  placeholder="選擇時間"
                  type="datetime"
                  :default-time="form.endTime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="狀態">
                <el-radio v-for="item in dict.sysStatus" :key="item.id" v-model="form.status" :label="item.dictKey">
                  {{ item.dictValue }}
                </el-radio>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序">
                <el-input-number
                  v-model.number="form.sort"
                  :min="0"
                  :max="999999"
                  controls-position="right"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="中文圖片">
                <el-upload
                  ref="my-upload"
                  :on-success="handleSuccessCn"
                  :on-error="handleError"
                  :headers="headers"
                  :action="imagesUploadApi"
                  :data="type"
                  :show-file-list="false"
                >
                  <img v-if="form.picCn" :src="baseApi+form.picCn" style="max-height: 150px;max-width: 220px">
                  <el-button v-else size="small" type="primary">點擊上傳</el-button>
                </el-upload>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="英文圖片">
                <el-upload
                  ref="my-upload"
                  :on-success="handleSuccessEn"
                  :on-error="handleError"
                  :headers="headers"
                  :action="imagesUploadApi"
                  :data="type"
                  :show-file-list="false"
                >
                  <img v-if="form.picEn" :src="baseApi+form.picEn" style="max-height: 150px;max-width: 200px">
                  <el-button v-else size="small" type="primary">點擊上傳</el-button>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="鏈接類型">
                <el-select v-model="form.linkType" placeholder="请选择鏈接類型" @change="selUrlType">
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item v-if="!form.linkType" label="鏈接页面">
                <el-select v-model="form.linkContentType" placeholder="请选择鏈接页面" @change="linkContentTypeChange">
                  <el-option
                    v-for="item in dict.internalUrlType"
                    :key="item.id"
                    :label="item.dictValue"
                    :value="item.dictKey"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item v-if="form.linkType" label="鏈接路徑">
                <el-input v-model="form.url" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col v-if="!form.linkType" :span="12">
              <el-form-item label="鏈接中文名稱">
                <el-input v-model="form.nameCn" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item v-if="!form.linkType" label="鏈接英文名稱">
                <el-input v-model="form.nameEn" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item v-if="!form.linkType" label="廣告商品">
                <el-virtual-transfer
                  v-model="form.productIds"
                  :data="productList"
                  :titles="['商品', '已選商品']"
                  target-order="unshift"
                  filterable
                  :props="{
                    key: 'id',
                    label: 'nameCn'
                  }"
                />
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">確認</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        row-key="id"
        stripe
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="advertiseType" label="廣告圖類型">
          <template slot-scope="scope">
            {{ dict.label.advertiseType[scope.row.advertiseType] }}
          </template>
        </el-table-column>
        <el-table-column prop="startTime" label="時間" width="200px">
          <template slot-scope="scope">
            <div>開始時間: {{ scope.row.startTime }}</div>
            <div>到期時間: {{ scope.row.endTime }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="picCn" label="中文圖片" width="200px">
          <template slot-scope="scope">
            <img :src="baseApi + scope.row.picCn" style="height: 80px" alt="">
          </template>
        </el-table-column>
        <el-table-column prop="picEn" label="英文圖片" width="200px">
          <template slot-scope="scope">
            <img :src="baseApi + scope.row.picEn" style="height: 80px" alt="">
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" />
        <el-table-column prop="status" label="狀態">
          <template slot-scope="scope">
            <mall-switch
              :row="scope.row"
              :dict-data="dict.label.sysStatus"
              :row-data="scope.row.status"
              row-filed="status"
              title="廣告圖顯示"
            />
          </template>
        </el-table-column>
        <el-table-column prop="note" label="備注" :show-overflow-tooltip="true" />
        <el-table-column
          v-if="checkPer(['admin','homeAdvertise:edit','homeAdvertise:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudHomeAdvertise from '@/api/sms/homeAdvertise'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getToken } from '@/utils/auth'
import { mapGetters } from 'vuex'
import mallSwitch from '@/components/mallSwitch'
import ElVirtualTransfer from '@/components/Transfer'
import depot from '@/api/wms/depot'
import { allProduct } from '@/api/pms/product'

const defaultForm = {
  id: null,
  advertiseType: 0,
  positionType: 0,
  note: null,
  startTime: null,
  endTime: null,
  picCn: null,
  picEn: null,
  url: null,
  sort: 0,
  status: true,
  productIds: [],
  linkType: 0,
  nameCn: '',
  nameEn: '',
  product: [],
  linkContentType: 0
}

export default {
  name: 'HomeAdvertise',
  components: { pagination, crudOperation, rrOperation, udOperation, mallSwitch, ElVirtualTransfer },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['advertiseType', 'sysStatus', 'internalUrlType'],
  cruds() {
    return CRUD({ title: '轮播图', url: 'homeAdvertise/page', sort: 'id:desc', crudMethod: { ...crudHomeAdvertise }})
  },
  data() {
    return {
      type: { type: 3 },
      AdUrlVisible: false,
      options: [{
        value: 1,
        label: '外部鏈接'
      }, {
        value: 0,
        label: '內部鏈接'
      }],
      permission: {
        add: ['admin', 'homeAdvertise:add'],
        edit: ['admin', 'homeAdvertise:edit'],
        del: ['admin', 'homeAdvertise:del']
      },
      productList: [],
      rules: {
        nameCn: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '开始时间不能为空', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '结束时间不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'nameCn', display_name: '名称' },
        { key: 'advertiseType', display_name: '广告类型' }
      ],
      headers: {
        'Authorization': getToken()
      }
    }
  },
  computed: {
    ...mapGetters(['imagesUploadApi', 'baseApi'])
  },
  created() {
    this.getPageList()
  },
  methods: {
    handleExceed() {
      this.$message.warning('当前限制选择 1张图片，请重新选择')
    },
    handleSuccessCn(res) {
      if (res.code === 0) {
        this.form.picCn = res.data.url
      } else {
        this.$message.error('图片上传失败,请重新上传')
      }
    },
    handleSuccessEn(res) {
      if (res.code === 0) {
        this.form.picEn = res.data.url
      } else {
        this.$message.error('图片上传失败,请重新上传')
      }
    },
    handleError() {
      this.$message.error('图片上传失败,请重新上传')
    },
    selUrlType(e) {
      this.form.linkContentType = null
      this.form.url = null
    },
    linkContentTypeChange(e) {
      this.form.product = []
      this.form.linkContentType = e
    },
    getPageList() {
      depot.getOnline().then(res => {
        if (res.code === 0) {
          this.onlineDepotId = res.data.id
          allProduct({
            deleteStatus: false,
            verifyStatus: true,
            publishStatus: 1,
            depotId: this.onlineDepotId,
            size: 9999
          }).then(res => {
            if (res.code === 0) {
              this.productList = res.data
            }
          })
        }
      })
    }
  }
}
</script>

<style  lang="scss" scoped>
>>> .el-transfer-panel {
  width: 500px !important;
}
</style>
